package org.wejde.muel;

import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * This class contains the members and methods related to
 * the header pane on the muel GUI.  The number and type of
 * JComponents including in the header pane is deteremined
 * by the configuration file.
 *  
 * @author Eric Martin
 * @author Webb Pinner
 *
 */
public class headerPane extends JPanel {

	/**
	 * Serial Version UID
	 */
	private static final long serialVersionUID = 6784013482489496880L;

	/**
	 * Default field delimiter to use
	 */
	private static final String DEFAULTDELIMETER = ",";

	/**
	 * Default field delimiter to use
	 */
	private static final String DEFAULTSEPERATOR = ":";

	/**
	 * Array of JComponents
	 */
	private JComponent[] jComponentArray;
	
	/**
	 * Array of Fields
	 */
	private fields[] fieldsArray;

	/**
	 * Delimiter to use between field values
	 */
	private String delim;
	
	/**
	 * Constructor method
	 * 
	 * @param fieldsArray the array of field objects to use
	 * to populate the headerpane
	 */
	public headerPane(fields[] fieldsArray) {
		super(new FlowLayout(FlowLayout.CENTER));
		this.setMinimumSize(new Dimension(800, 200));
		this.setSize(this.getMinimumSize());
		this.delim = new String(headerPane.DEFAULTDELIMETER);
		this.fieldsArray = fieldsArray;
		jComponentArray = new JComponent[this.fieldsArray.length];
		
		// Loop through the fields array and setup the
		// checkboxes
		for( int i=0; i< this.fieldsArray.length; i++ ) {
			
			if(this.fieldsArray[i] != null && this.fieldsArray[i].getFieldType() == fields.CHECKBOX) {
				// DEBUG
				//System.err.println(this.fieldsArray[i].getFieldLabel());

				// Build the JComponent
				JCheckBox cb = new JCheckBox(this.fieldsArray[i].getFieldLabel());
				cb.setToolTipText(this.fieldsArray[i].getFieldTooltip());
				jComponentArray[i] = cb;
				this.add(jComponentArray[i]);				
			}
		
		// Loop through the fields array and setup the
		// textfields
			if(this.fieldsArray[i] != null && this.fieldsArray[i].getFieldType() == fields.TEXTFIELD) {
				// DEBUG
				//System.err.println(this.fieldsArray[i].getFieldLabel());
				JTextField tf = new JTextField(this.fieldsArray[i].getFieldValue()[0]);
				tf.setToolTipText(this.fieldsArray[i].getFieldTooltip());
				tf.setColumns(8);
				jComponentArray[i] = tf;
				JLabel l = new JLabel(this.fieldsArray[i].getFieldLabel());
				this.add(l);
				this.add(jComponentArray[i]);
			}

		// Loop through the fields array and setup the
		// comboboxes
			if(this.fieldsArray[i] != null && this.fieldsArray[i].getFieldType() == fields.COMBOBOX) {
				// DEBUG
				//System.err.println(this.fieldsArray[i].getFieldLabel());
				JComboBox cb = new JComboBox(this.fieldsArray[i].getFieldValue());
				cb.setToolTipText(this.fieldsArray[i].getFieldTooltip());
				cb.setEditable(false);
				jComponentArray[i] = cb;
				JLabel l = new JLabel(this.fieldsArray[i].getFieldLabel());
				this.add(l);
				this.add(jComponentArray[i]);
			}
		}
	}
	
	/**
	 * Collect the values and hidden values of the
	 * JComponents.  The values are deliminated using the
	 * objects delim variable.
	 * 
	 * @return the string of values and hidden values.
	 */
	public String getFieldValues() {
		
		String msg = "";
		
		for(int i=0; i<this.jComponentArray.length; i++) {
			// Code for Checkboxes
			if(this.fieldsArray[i].getFieldType()==fields.CHECKBOX) {
				JCheckBox cb = (JCheckBox)jComponentArray[i];
				if(cb.isSelected()) {
					msg += fieldsArray[i].getFieldHiddenValue()[0] + this.delim;
				}
			}
			// Code for ComboBoxes
			if(fieldsArray[i].getFieldType()==fields.COMBOBOX) {
				JComboBox cb = (JComboBox)jComponentArray[i];
				if (cb.getSelectedIndex() > 0) {
					if (!fieldsArray[i].getFieldHiddenValue()[cb.getSelectedIndex()].equals("")) {
						if(fieldsArray[i].isUseOnlyHidden()) {
							msg += fieldsArray[i].getFieldHiddenValue()[cb.getSelectedIndex()] + this.delim;
						}
						else {
							msg += fieldsArray[i].getFieldHiddenValue()[cb.getSelectedIndex()] + headerPane.DEFAULTSEPERATOR;
							msg += fieldsArray[i].getFieldValue()[cb.getSelectedIndex()] + this.delim;
						}
//						if (fieldsArray[i].getFieldHiddenValue()[cb.getSelectedIndex()].endsWith(headerPane.DEFAULTSEPERATOR)) {
//							msg += fieldsArray[i].getFieldHiddenValue()[cb.getSelectedIndex()] + this.delim;
//						} else {
//							msg += fieldsArray[i].getFieldHiddenValue()[cb.getSelectedIndex()] + headerPane.DEFAULTSEPERATOR;
//							msg += fieldsArray[i].getFieldValue()[cb.getSelectedIndex()] + this.delim;
//						}
					} else {
						msg += fieldsArray[i].getFieldValue()[cb.getSelectedIndex()] + this.delim;
					}
				}
			}
			// Code for TextFields
			if(fieldsArray[i].getFieldType()==fields.TEXTFIELD) {
				JTextField tf = (JTextField)jComponentArray[i];
				if (tf.getText().length() > 0) {
					if (fieldsArray[i].getFieldHiddenValue()[0].length() > 0) {
						msg += fieldsArray[i].getFieldHiddenValue()[0] + headerPane.DEFAULTSEPERATOR;
					}
					msg += tf.getText() + this.delim;
				}
			}
		}
		return msg;
	}

	/**
	 * Clear/Reset the values for the JComponents to their
	 * default state.
	 */
	public void clearFields() {
		for(int i=0; i<this.jComponentArray.length; i++) {
			if(this.fieldsArray[i].isFieldClearOnSubmit()) {
				// Code for Checkboxes
				if(fieldsArray[i].getFieldType()==fields.CHECKBOX) {
					JCheckBox cb = (JCheckBox)jComponentArray[i];
					if(fieldsArray[i].getFieldValue()[0] == fields.TRUE) {
						cb.setSelected(true);
					} else {
						cb.setSelected(false);
					}
				}
				// Code for ComboBoxes
				if(fieldsArray[i].getFieldType()==fields.COMBOBOX) {
					JComboBox cb = (JComboBox)jComponentArray[i];
					cb.setSelectedIndex(0);
				}
				// Code for TextFields
				if(fieldsArray[i].getFieldType()==fields.TEXTFIELD) {
					JTextField tf = (JTextField)jComponentArray[i];
					tf.setText(fieldsArray[i].getFieldValue()[0]);
				}
			}
		}
	}
	
	/**
	 * Set the delimiter to use when collecting the values
	 * from the JComponets.
	 * 
	 * @param delim
	 */
	public void setDelimiter(String delim) {
		this.delim = delim;
	}

	/**
	 * Get the delimiter used to seperate the values from
	 * the JComponents.
	 * 
	 * @return String containing the delimiter
	 */
	public String getDelimiter() {
		return this.delim;
	}
}
